package com.boilerCloud.dao.msg;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import com.boilerCloud.dao.BaseDao;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.msg.AppMessages;
import com.boilerCloud.dto.msg.PushMessages;
import com.boilerCloud.dto.sys.ProtocolInfo;
import com.boilerCloud.util.StringUtil;

@Repository
public class PushDao extends BaseDao {

	public void save(final PushMessages msg)throws Exception {
		final String sql = "insert into push_messages(title,content,type,receive_type,status) values(?,?,?,?,1)";
		KeyHolder keyHolder = new GeneratedKeyHolder();
		this.jdbcTemplate.update(new PreparedStatementCreator() {
			public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
				PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
				ps.setString(1, msg.getTitle());
				ps.setString(2, msg.getContent());
				ps.setInt(3, msg.getType());
				ps.setInt(4, msg.getReceiveType());
				return ps;
			}
		}, keyHolder);
		msg.setId(keyHolder.getKey().intValue());
	}

	public void qryList(PushMessages device, HzlqswPage<PushMessages> page) throws Exception{
		String countsql = "select count(*) from push_messages a where 1= 1";
				
			
		String listSql = "select a.*  "
					+ " from push_messages a where 1= 1";
					//+ " left join receive_messages b on a.id = b.push_id "
					//+ " left join app_user c on c.id = b.receive_id ";
					
		List<Object> params= new ArrayList<Object>();
		if(!StringUtil.isEmpty(device.getTitle())){
			countsql += " and a.title like ?";
			listSql += " and a.title like ?";
			params.add("%" + device.getTitle() +"%");
		}
			
		listSql += " order by id desc ";
		this.queryListByPage(countsql, params, listSql, params, page, PushMessages.class);
		
	}

	public void addPushRecord(int msg_id, int receiveId) {
		String sql = "insert into receive_messages(push_id,receive_id,status) values(?,?,1)";
		this.jdbcTemplate.update(sql,msg_id,receiveId);
		
	}

	public void replyFeedbackMsg(PushMessages msg)throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void qryFeedbackMsgList(AppMessages msg, HzlqswPage<AppMessages> page) {
		// TODO Auto-generated method stub
		
	}

	public List<String> getReceiveName(int id)throws Exception {
		String sql = "select a.name from app_user a "
				+ "left join receive_messages b on a.id = b.receive_id"
				+ " where b.push_id = ?";
		return this.queryForList(sql, String.class,id);
	}

	public void del(int id) {
		String  sql = "delete from push_messages where id =?";
		this.jdbcTemplate.update(sql,id);
		
	}

}
